package com.lagou.interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lagou.vo.MachineStatus;
import com.lagou.vo.ResponseContent;
import com.lagou.vo.UserInfo;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;

/**
 * @author: wudong
 * @create: 2021-08-03 16:35
 **/
public class RequestInterceptor  implements HandlerInterceptor {

    /**
     * 之前执行（进入Handler处理之前）
     * 可以进行权限验证
     * @param request
     * @param response
     * @param handler
     * @return  true放行，false中止程序
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        System.out.println("==========>>>>sessionId:" + session.getId());
        System.out.println("======SESSION::::" + session);

        Enumeration<String> attrs = session.getAttributeNames();
        // 遍历attrs中的
        while(attrs.hasMoreElements()){
            // 获取session键值
            String name = attrs.nextElement().toString();
            // 根据键值取session中的值
            Object vakue = session.getAttribute(name);
            // 打印结果
            System.out.println("------" + name + ":" + vakue +"--------\n");

        }

        System.out.println("===============>>>>>>当前uri：" + request.getRequestURI());
        if(request.getRequestURI().startsWith("/user")){
            UserInfo userInfo = UserInfo.buildFromSession(session);
            if(userInfo == null){
                System.out.println("未登录，请登录");

                ResponseContent responseContent = ResponseContent.buildErrorResponseContent(401, "未登录，请登录");
                responseContent.setValue(MachineStatus.build(request,session));

                ObjectMapper mapper = new ObjectMapper();
                String json = mapper.writeValueAsString(responseContent);
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().print(json);

                return false;
            }
            System.out.println("已登录，放行请求");
            return true;
        }else {
            return true;
        }
    }

    /**
     * 之中执行（Handler处理完毕但尚未跳转页面）
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    /**
     * 之后执行（Handler处理完毕而且已经跳转页面）
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
